package lion.dev.text;

import java.util.HashMap;
import java.util.Map;

/**
 * 将汉字大写金额转为数字
 *
 * <pre>
 * time   2014 2014年2月24日 下午4:20:42
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion
 */
public class CNMoneyFormater {

	private static String name = "壹贰叁肆伍陆柒捌玖";
	private static int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	private static String unit = "拾佰仟万亿";

	private static Map<String, Double> umap = new HashMap<>();
	static {
		umap.put("拾", 10d);
		umap.put("佰", 100d);
		umap.put("仟", 1000d);
	}

	public static double parseMoney(String src) {

		if (name.indexOf(src.charAt(0)) < 0) {
			src = "壹" + src;
		}

		double sum = 0;

		String u = "";
		String n = "";
		double level = 1;
		for (int i = src.length() - 1; i >= 0; i--) {

			char c = src.charAt(i);
			if (c == '万') {
				level *= 10000;
				continue;
			} else if (c == '亿') {
				level = 100000000;
				continue;
			}

			if (unit.indexOf(c) > -1) {
				u = c + u;
			} else if (name.indexOf(c) > -1) {
				n = c + "";
				double tmp = nums[name.indexOf(n)] * level;
				Double d = umap.get(u);
				if (d != null) {
					tmp *= d;
				}
				sum += tmp;
				u = "";
			}
		}
		return sum;
	}
}
